package drh.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.handlers.ScalarHandler;

import drh.bean.Address;
import drh.dao.AddressDao;
import drh.dao.BaseDao;
import handler.AddressResultSetHandler;

public class AddressDaoImpl extends BaseDao implements  AddressDao {

	@Override
	public Integer deleteAddressById(Integer id) {
		try {
			return query.update("delete from address where id=?",id);
		} catch (SQLException e) {
		
			e.printStackTrace();
		}
		return -1;
	}

	@Override
	public Integer updateAddress(Address info) {
	try {
		return query.update("updata address set userid=?,address=?,isdfault=?,consignee=?,tel=?,time=?,isdelete=? where id=?",
		info.getUserid(),info.getAddress(),info.getIsdfault(),info.getConsignee(),info.getTel(),info.getTime(),info.getIsdelete());
	} catch (SQLException e) {
		e.printStackTrace();
	}
	
		return -1;
	}

	@Override
	public Integer addAddress(Address info) {
	try {
		return query.update("insert into address(userid,address,isfault,consignee,tel,time,isdelete) values(?,?,?,?,?,?,?");
	} catch (SQLException e) {
		e.printStackTrace();
	}
		return -1;
	}

	@Override
	public List<Address> GetAddressPaging(Integer pageIndex, Integer pageSize) {
		try {
			return query.query("select * from address limit ?,?", new AddressResultSetHandler(),(pageIndex-1)*pageSize,pageSize);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	@Override
	public Integer getCount() {
		String sql = "select count(1) from collection";
		Object query2;
		try {
			 query2 = query.query(sql, new ScalarHandler());
			return Integer.parseInt(query2.toString());
		} catch (SQLException e) {
	
			e.printStackTrace();
		}
		
		return null;
	}


	
}
